/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battleshipgame.logic;
import battleshipgame.logic.Player.Direction;
import battleshipgame.logic.Coordinate;
import battleshipgame.logic.InvalidInputException;
import battleshipgame.logic.Square;
import java.util.HashMap;
/**
 */
public class Board {
    
   
    
    private Square[][]  matrix; 
   //private HashMap<Battleship, Square[]> BattleshipTypeAndPositionMap;// maps the whereabout of each battleship
   private Square nullSquare ; 
   public enum SquareStatus {BATTLESHIP, BOMBED_BATTLESHIP, SEA, BOMBED_SEA }
    public Board() {
        this.matrix = new Square[Constants.BOARD_SIZE][Constants.BOARD_SIZE];
        int i ,k;
        for ( i=0 ; i < Constants.BOARD_SIZE ; i ++)
            for (k=0 ; k < Constants.BOARD_SIZE ; k++)
                matrix [i][k] = new Square();
    }
    public boolean  bombByCoordinate ( String coordinateString , Player attackedPlayer )  throws InvalidInputException, AlreadyBombedException{
        boolean res;
        Coordinate coordinate = Coordinate.ConvertStringToXY(coordinateString);// Converting using constractor from String to coordinate
        Square temp =  getSquareByCoordinate(coordinate); // bombMe is a method in square.
        return temp.Bomb(attackedPlayer);
    }
    public SquareStatus getSquareStatusByCoordinate (String coordinateString)   throws InvalidInputException {//For Printing purposes
        Coordinate coordinate = Coordinate.ConvertStringToXY(coordinateString);// Converting using constractor from String to coordinate
        Square resSquare = getSquareByCoordinate(coordinate) ;
        return resSquare.getStatus();
    }
    public Square getSquareByCoordinate (String coordinateString)   throws InvalidInputException {//For Printing purposes
        Coordinate coordinate = Coordinate.ConvertStringToXY(coordinateString);// Converting using constractor from String to coordinate
        Square resSquare = getSquareByCoordinate(coordinate) ;
        return resSquare;
    }
// * This method returns the necessary square in the board by it's coordinate
    public Square getSquareByCoordinate(Coordinate coordinate) {
            Square s = this.matrix[coordinate.getX()][coordinate.getY()];
            return s;
    }
    public void setBattleshipInSquareByCoordinate ( Battleship battleship , Coordinate coordinate  , Direction direction)   /*throws Exception */{ //eliran
      getSquareByCoordinate(coordinate).setBattleship(battleship);
      getSquareByCoordinate(coordinate).setStatus(SquareStatus.BATTLESHIP);
      int X =  coordinate.getX() ;
      int Y =  coordinate.getY() ;
        int Xoffset , Yoffset ;
        for ( Xoffset = -1 ; Xoffset <= 1 ; Xoffset ++ ) 
            for ( Yoffset = -1 ; Yoffset<=1 ; Yoffset ++  )
                if ( new Coordinate(Xoffset + X , Yoffset + Y).IsValidLocationInBoard())
                    getSquareByCoordinate(new Coordinate(Xoffset + X , Yoffset + Y)).setShipToBeUnsettable() ;
    }
    public boolean  IsBattleshipValid (Battleship battleship , Coordinate coordinate , Direction direction) {
        int size = battleship.getSize() ; 
        int i;
        int x = coordinate.getX();
        int y = coordinate.getY();
        boolean flag = true ;
          if ( direction == Direction.HORIZONTAL)  {
              if ( x + size > Constants.BOARD_SIZE ) 
                  return false ;
              for (  i = 0 ; i < size ; i ++ ) {//checking if coordinate is out of bounds
                  flag = ( getSquareByCoordinate(new Coordinate(x, y)).isShipSettable() && flag ) ;
                  x++; // Eliran: move coordinate one step right
              }
              return flag;
          }else{//direction == Direction.VERTICAL
              if ( y + size > Constants.BOARD_SIZE ) 
                  return false ;
              for (  i = 0 ; i < size ; i ++ ) {//checking if coordinate is out of bounds
                  flag = ( getSquareByCoordinate(new Coordinate(x, y)).isShipSettable() && flag ) ;
                  y++; // Eliran: move coordinate one step right
              }
              return flag;
          }
    }
       
    public void InsertBattleship(Battleship battleship , String coordinateString , Direction direction) throws InvalidInputException {
        /* algorithm : 
         *       if Horizontal then put horizonatlly the ship
         *          else put vertically
         */
        int size = battleship.getSize() ; 
        int i;
        Coordinate coordinate = Coordinate.ConvertStringToXY(coordinateString);// Converting using constractor from String to coordinate
        if (!IsBattleshipValid(battleship, coordinate, direction))
            throw new InvalidInputException (  "Out Of Board Bounds. Board" )  ;//check if valid location on //TODO: complete this throw
        Square tempSquare = getSquareByCoordinate(coordinate) ; //tempsquare
        // all the squares that you are about to set ships into them. 
        if ( direction == Direction.HORIZONTAL)  {
            for (  i = 0 ; i < size ; i ++ ) {
                tempSquare.setBattleship(battleship);
                setBattleshipInSquareByCoordinate(battleship, coordinate, direction);
                coordinate.setX(coordinate.getX() + 1); // Eliran: move coordinate one step right
            }
        }
        else { // direction == Direction.VERTICAL 
            for (  i = 0 ; i < size ; i ++ ) {//checking if coordinate is out of bounds
                setBattleshipInSquareByCoordinate(battleship, coordinate, direction);
                                coordinate.setY(coordinate.getY() + 1); // Eliran: move coordinate one step right
            }
        }
    }
} // end of board
        //boolean validLocation = true ;

